﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace TopicModeConsume
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var channel = Common.RabbitMQ.GetChannel();

            CreateConsume(Convert.ToInt32(Console.ReadLine()), channel, Console.ReadLine());

            Console.ReadKey();
        }

        static void CreateConsume(int i, IModel channel, string routingKey)
        {
            //交换机
            channel.ExchangeDeclare("mytopicexchange", ExchangeType.Topic, true, false, default);

            //队列
            channel.QueueDeclare($"mytopicqueue{i}", true, false, false, default);

            //绑定
            channel.QueueBind($"mytopicqueue{i}", "mytopicexchange", routingKey, default);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (sender, e) =>
            {
                Console.WriteLine(Encoding.UTF8.GetString(e.Body.ToArray()));
            };

            channel.BasicConsume($"mytopicqueue{i}", true, consumer);

            Console.WriteLine($"consumer{i} {routingKey} started");

        }
    }
}
